﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.LogEntries.ClanChangeKingdomLogEntry
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using Helpers;
using System.Collections.Generic;
using TaleWorlds.Core;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.LogEntries
{
  public class ClanChangeKingdomLogEntry : LogEntry, IChatNotification, IWarLog
  {
    [SaveableField(150)]
    public readonly Clan Clan;
    [SaveableField(151)]
    public readonly Kingdom OldKingdom;
    [SaveableField(152)]
    public readonly Kingdom NewKingdom;
    [SaveableField(153)]
    private readonly bool _byRebellion;

    internal static void AutoGeneratedStaticCollectObjectsClanChangeKingdomLogEntry(
      object o,
      List<object> collectedObjects)
    {
      ((LogEntry) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
      collectedObjects.Add((object) this.Clan);
      collectedObjects.Add((object) this.OldKingdom);
      collectedObjects.Add((object) this.NewKingdom);
    }

    internal static object AutoGeneratedGetMemberValueClan(object o)
    {
      return (object) ((ClanChangeKingdomLogEntry) o).Clan;
    }

    internal static object AutoGeneratedGetMemberValueOldKingdom(object o)
    {
      return (object) ((ClanChangeKingdomLogEntry) o).OldKingdom;
    }

    internal static object AutoGeneratedGetMemberValueNewKingdom(object o)
    {
      return (object) ((ClanChangeKingdomLogEntry) o).NewKingdom;
    }

    internal static object AutoGeneratedGetMemberValue_byRebellion(object o)
    {
      return (object) ((ClanChangeKingdomLogEntry) o)._byRebellion;
    }

    public bool IsVisibleNotification => true;

    public override ChatNotificationType NotificationType
    {
      get => this.MilitaryNotification((IFaction) this.NewKingdom, (IFaction) this.OldKingdom);
    }

    public ClanChangeKingdomLogEntry(
      Clan clan,
      Kingdom oldKingdom,
      Kingdom newKingdom,
      bool byRebellion)
    {
      this.Clan = clan;
      this.OldKingdom = oldKingdom;
      this.NewKingdom = newKingdom;
      this._byRebellion = byRebellion;
    }

    public bool IsRelatedToWar(StanceLink stance, out IFaction effector, out IFaction effected)
    {
      IFaction faction1 = stance.Faction1;
      IFaction faction2 = stance.Faction2;
      effector = this.NewKingdom?.MapFaction;
      effected = this.OldKingdom?.MapFaction;
      if (this.OldKingdom == faction1 && this.NewKingdom == faction2)
        return true;
      return this.OldKingdom == faction2 && this.NewKingdom == faction1;
    }

    public TextObject GetNotificationText()
    {
      if (this.NewKingdom == null)
        return TextObject.Empty;
      TextObject text = GameTexts.FindText("str_notification_change_faction");
      text.SetTextVariable("FACTION", this.NewKingdom.InformalName);
      text.SetTextVariable("CLAN_NAME", this.Clan.Name);
      return text;
    }

    public override ImportanceEnum GetImportanceForClan(Clan clan)
    {
      if (clan == this.Clan)
        return ImportanceEnum.ReasonablyImportant;
      return clan.Kingdom != null && (clan.Kingdom == this.OldKingdom || clan.Kingdom == this.NewKingdom) ? ImportanceEnum.SomewhatImportant : ImportanceEnum.Zero;
    }

    public override void GetConversationScoreAndComment(
      Hero talkTroop,
      bool findString,
      out string comment,
      out ImportanceEnum score)
    {
      score = ImportanceEnum.Zero;
      comment = "";
      if (this.Clan != Clan.PlayerClan || !talkTroop.IsLord)
        return;
      StringHelpers.SetCharacterProperties("TT_LIEGE", talkTroop.MapFaction.Leader.CharacterObject);
      if (talkTroop.MapFaction == this.NewKingdom && Hero.MainHero.MapFaction == talkTroop.MapFaction && Hero.MainHero != this.NewKingdom.Leader && talkTroop != this.NewKingdom.Leader)
      {
        score = ImportanceEnum.ReasonablyImportant;
        if (!findString)
          return;
        if (Hero.MainHero.Clan.IsUnderMercenaryService)
        {
          comment = "str_comment_changeherofaction_you_joined_us_as_mercenary";
        }
        else
        {
          this.NewKingdom.Leader.SetTextVariables();
          comment = "str_comment_changeherofaction_you_joined_us";
        }
      }
      else if (talkTroop.MapFaction == this.OldKingdom && Hero.MainHero.MapFaction != talkTroop.MapFaction && this._byRebellion)
      {
        score = ImportanceEnum.QuiteImportant;
        if (!findString)
          return;
        this.OldKingdom.Leader.SetTextVariables();
        if (this.OldKingdom.Leader == talkTroop)
        {
          comment = "str_comment_changeherofaction_you_rebelled_against_me";
        }
        else
        {
          comment = "str_comment_changeherofaction_you_rebelled_against_us";
          score = ImportanceEnum.VeryImportant;
        }
      }
      else
      {
        if (talkTroop.MapFaction != this.OldKingdom || Hero.MainHero.MapFaction == talkTroop.MapFaction || talkTroop == this.OldKingdom.Leader)
          return;
        score = ImportanceEnum.ReasonablyImportant;
        if (!findString)
          return;
        comment = "str_comment_changeherofaction_you_renounced_your_allegiance";
      }
    }

    public override string ToString() => this.GetNotificationText().ToString();
  }
}
